﻿#region Using

using GameLogic.External;

#endregion

namespace GameLogic.Internal
{

    #region Using

    #endregion

    internal class Barrack : Building
    {
        public Barrack(Tile tile)
            : base(tile, BuildingType.Barrack)
        {
            this.ProgressTimer.Elapsed += this.OnProgressTimerElapsed;
        }

        internal override void Update(GameTime gameTime)
        {
            if (this.ProgressTimer.Enabled)
            {
                this.ProgressTimer.Update(gameTime);
            }
            else
            {
                if (this.FarmerQueue.Count != 0)
                {
                    if (this.Tile.Owner.GoldAmount >= this.Settings.GoldCostsForMilitia)
                    {
                        this.Tile.Owner.GoldAmount -= this.Settings.GoldCostsForMilitia;
                        this.FarmerQueue.Dequeue();
                        this.ProgressTimer.Start(this.Settings.BarrackProductionTime);
                    }
                }
            }
        }

        internal override void Reset()
        {
            this.FarmerQueue.Clear();
            this.ProgressTimer.Stop();
            this.ProgressTimer.Reset();
        }

        private void OnProgressTimerElapsed(ProgressTimer source)
        {
            new Militia(this.Tile.Owner).ArriveAt(this.Tile);
        }
    }
}